<?php
namespace app\admin\controller;
// use think\Db;
// use think\captcha\Captcha;
class Test extends Come{
    public $token;
    public function _initialize(){
        $this->token=cache('access_token');
        if($this->token==false){
            $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx2349ac57926cd580&secret=e28531e0fb6784ea37d4590882fc90c9";

            $this->token=get($url);
            cache('access_token',$this->token,7200);
        }
        $this->token=json_decode($this->token,true);
    }
    public function test(){
        return $this->fetch('test');
    }
    public function wx(){

        // $url1="https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$this->token['access_token']}";

        // $text=[
        //     "touser"=>"o_8541AIsZi6HqH2uywHWn3fA9rw",
        //     "msgtype"=>"text",
        //     "text"=>[
        //         "content"=>"Hello World",
        //     ]
        // ];
       
        $url1="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={$this->token['access_token']}";
        $text=[
            "touser"=>"o_8541AIsZi6HqH2uywHWn3fA9rw",
            "template_id"=>"quH0xB_AbhmYWUIKUDwFxnNLM3tl2lFBpfxorTB17Fo",
            "url"=>"http://weixin.qq.com/download",  
            
            "data"=>[
                "first"=> [
                    "value"=>"恭喜你购买成功！",
                    "color"=>"#173177"
                ],
                "title"=> [
                    "value"=>"Iphone8",
                    "color"=>"#173177"
                ],
                "money"=> [
                    "value"=>"7100！",
                    "color"=>"#173177"
                ],   
            ]
        ];
        $text=json_encode($text);
        $ret=post($url1,$text);
        dump($ret);
    }
   
    public function upload(){
        $url="https://api.weixin.qq.com/cgi-bin/media/upload?access_token={$this->token['access_token']}&type=image";
        $file=request()->file('image');
            if($file){
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                if($info){
                        $path=$info->getSaveName();
                        $img_src=ROOT_PATH.'public/uploads/'.$path;

                        $data=array(
                            "media"=>new \CURLFile($img_src),
                        );
                        $ret=post($url,$data);
                        $media_ids=json_decode($ret,true);
                        dump($media_ids);
                        $media_id=$media_ids['media_id'];
                        dump($ret);
                        $img=[
                            "touser"=>"o_8541AIsZi6HqH2uywHWn3fA9rw",
                            "msgtype"=>"image",
                            "image"=>[
                                "media_id"=>$media_id,
                            ]
                        ];
                    $img=json_encode($img);
                    $url2="https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$this->token['access_token']}";
                    $ret=post($url2,$img);
                    dump($ret);
                }else{
                    $this->error($file->getError()); 
                }
            }
            else{
                $this->error('必须上传图片！');
            }
    }

    public function upvoice(){
        $url="https://api.weixin.qq.com/cgi-bin/media/upload?access_token={$this->token['access_token']}&type=voice";
        $file=request()->file('voice');
            if($file){
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                if($info){
                        $path=$info->getSaveName();
                        $voice=ROOT_PATH.'public/uploads/'.$path;

                        $data=array(
                            "media"=>new \CURLFile($voice),
                        );
                        $ret=post($url,$data);
                        $media_ids=json_decode($ret,true);
                        dump($media_ids);
                        $media_id=$media_ids['media_id'];
                        dump($ret);
                        $voice1=[
                            "touser"=>"o_8541AIsZi6HqH2uywHWn3fA9rw",
                            "msgtype"=>"voice",
                            "voice"=>[
                                "media_id"=>$media_id,
                            ]
                        ];
                    $voice1=json_encode($voice1);
                    $url2="https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$this->token['access_token']}";
                    $ret=post($url2,$voice1);
                    dump($ret);
                }else{
                    $this->error($file->getError()); 
                }
            }
            else{
                $this->error('必须上传语音！');
            }
    }

    public function upvideo(){
        $url="https://api.weixin.qq.com/cgi-bin/media/upload?access_token={$this->token['access_token']}&type=video";
        $file=request()->file('video');
            if($file){
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                if($info){
                        $path=$info->getSaveName();
                        $video=ROOT_PATH.'public/uploads/'.$path;

                        $data=array(
                            "media"=>new \CURLFile($video),
                        );
                        $ret=post($url,$data);
                        $media_ids=json_decode($ret,true);
                        dump($media_ids);
                        $media_id=$media_ids['media_id'];
                        dump($ret);
                        $video1=[
                            "touser"=>"o_8541AIsZi6HqH2uywHWn3fA9rw",
                            "msgtype"=>"video",
                            "video"=>[
                                "media_id"=>$media_id,
                            ]
                        ];
                    $video1=json_encode($video1);
                    $url2="https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$this->token['access_token']}";
                    $ret=post($url2,$video1);
                    dump($ret);
                }else{
                    $this->error($file->getError()); 
                }
            }
            else{
                $this->error('必须上传视频！');
            }
    }

    public function mem(){
        $data=Cache::store('memcache')->get('name1');
        if($data==false){
            echo "没有缓存";
            $data="nikeadidas";
            Cache::store('memcache')->set('name1',$data,10);
        }
        dump($data);
    }
    
}
